﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using TheGameClient.Webservices;
using Waaahsabi.Controls.DWM;

namespace TheGameClient
{
	public partial class MainPage
	{
		private SmallComputerSystem _targetHost;

		private void worldMapButton_OnClick(object sender, MouseButtonEventArgs e)
		{
			DlgMainMapWindow map = new DlgMainMapWindow();
			map.Closed += delegate
			{
				if (map.DialogResult == true)
				{
					//make connection to target host
					List<ulong> nodes = map.ConnectedNodes;
					_targetHost = map.TargetSystem;
					App.Client.Connect_ConnectToSystemCompleted += ClientConnectConnectToSystemCompleted;
					App.Client.Connect_ConnectToSystemAsync(App.InternalSessionId, nodes);
				}
			};
			map.Show();
		}

		private void ClientConnectConnectToSystemCompleted(object sender, Connect_ConnectToSystemCompletedEventArgs e)
		{
			SmallComputerSystem thisTargetHost = _targetHost;
			App.Client.Connect_ConnectToSystemCompleted -= ClientConnectConnectToSystemCompleted;
			if (e.Error != null)
			{
				App.ServerError();
				return;
			}
			if (e.Result == null)
			{
				MessageBox.Show(string.Format("Connection to {0} ({1}) failed.", thisTargetHost.Name,
				                              thisTargetHost.Corporation.Name));
				return;
			}
			Guid connectionId = e.Result.Value;
			GenericSystemInterface systemInterface = new GenericSystemInterface();
			systemInterface.Initialize(
				thisTargetHost.Id,
				e.Result.Value,
				string.Format("{0} ({1})", thisTargetHost.Name, thisTargetHost.Corporation.Name),
				string.Format(
					"You are accessing {0} of {1}. By logging into this system you agree to follow the Terms of Use of {1}'s computer systems. Any violation or unauthorized access will be logged and strictly prosecuted.",
					thisTargetHost.Name, thisTargetHost.Corporation.Name)
				);
			systemInterface.ShowWelcomeScreen();
			TheGameWindow connectionWindow =
				_desktop.CreateWindow(string.Format("Conn. to {0} ({1})", thisTargetHost.Name, thisTargetHost.Corporation.Name),
				                      systemInterface);
			connectionWindow.AllowMaximize = true;
			connectionWindow.AllowMinimize = true;
			connectionWindow.SetSize(new Size(800, 480));
			connectionWindow.OnClosing += delegate
			{
				systemInterface.Close();
				App.Client.Connect_DisconnectFromSystemCompleted += client_Connect_DisconnectFromSystemCompleted;
				App.Client.Connect_DisconnectFromSystemAsync(App.InternalSessionId, connectionId);
			};
			systemInterface.Disconnect += delegate { connectionWindow.CloseInstantly(); };
			connectionWindow.Show();
		}

		private void client_Connect_DisconnectFromSystemCompleted(object sender,
		                                                          Connect_DisconnectFromSystemCompletedEventArgs e)
		{
			App.Client.Connect_DisconnectFromSystemCompleted -= client_Connect_DisconnectFromSystemCompleted;
			if (e.Error != null)
			{
				App.ServerError();
				return;
			}
		}
	}
}